//
//  ViewControllerForPhotoCollectDetail.m
//  世界
//
//  Created by dllo on 16/3/1.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "ViewControllerForPhotoCollectDetail.h"
#import "DataBaseForPhoto.h"
#import <UIImageView+WebCache.h>
#import "ModelForForPhotoDetail.h"

@interface ViewControllerForPhotoCollectDetail ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIImageView *imgView;
@property (nonatomic, retain) UITextView *textView;
@property (nonatomic, retain) UILabel *lableForTitle;
@property (nonatomic, retain) UILabel *lableForPage;

@property (nonatomic, retain) NSMutableArray *arrForModels;
@property (nonatomic, assign) NSInteger path;

@end

@implementation ViewControllerForPhotoCollectDetail

-(void)dealloc
{
    [_scrollView release];
    
    [_imgView release];
    
    [_textView release];
    
    [_lableForTitle release];
    
    [_lableForPage release];
    
    
    [_arrForModels release];
    
    [super dealloc];
}

#pragma mark 视图的生命周期 >>>>>>
#pragma mark 视图加载完成
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [[DataBaseForPhoto shareDataBaseHandle]open];
    [[DataBaseForPhoto shareDataBaseHandle] createTable];
    
    self.arrForModels = [[DataBaseForPhoto shareDataBaseHandle] select];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.navigationItem.title = @"详情";
    
    UIColor *bg = [UIColor colorWithPatternImage:[UIImage imageNamed:@"图片背景.jpg"]];
    
    self.view.backgroundColor = bg;
    
    //添加UITextView
    [self createTextView];
    
    //添加详细内容的标题和页数
    [self createLableForTitleAndPage];
    
    [self creataScorollView];
}




#pragma mark 收到内存警告!!!
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}




#pragma mark 自定义方法 >>>>>>



#pragma mark 创建UIscrollView
- (void)creataScorollView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
    
    self.scrollView.center = self.view.center;
    
    //开启翻页效果
    self.scrollView.pagingEnabled = YES;
    
    //指定代理人
    self.scrollView.delegate = self;
    
    [self.view addSubview:self.scrollView];
    
    [_scrollView release];
    
    self.scrollView.backgroundColor = [UIColor whiteColor];
    
    //设置scroll的内容尺寸
    self.scrollView.contentSize =CGSizeMake(self.scrollView.frame.size.width * self.arrForModels.count, 300);
    
    //根据scrollView的内容偏移量计算出当前的页数
    
    self.scrollView.contentOffset = CGPointMake(self.number * 414, 0);
   
    
    //将图片添加的scrollView上
    for (int i = 0; i < self.arrForModels.count; i++)
    {
        
        ModelForForPhotoDetail *model2 = [self.arrForModels objectAtIndex:i];
        
        
        self.imgView = [[UIImageView alloc] init];
        
        //解析图片的网址,将图片添加到imageView上
        [self.imgView sd_setImageWithURL:[NSURL URLWithString:model2.url]];
        
        
        //设置imageView的位置及大小 添加的相应的内容视图上
        self.imgView.frame = CGRectMake(self.scrollView.frame.size.width * i, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
        
        [self.scrollView addSubview:self.imgView];
        
        [_imgView release];
    }
    
    ModelForForPhotoDetail *model1 = self.arrForModels[self.number];
    
    NSString *strForContent = model1.content;
    
    NSString *strForAuthor = model1.author;
    
    self.textView.text = [NSString stringWithFormat:@"%@\n(摄影:%@)", strForContent,strForAuthor];
    
    //显示详细内容
    self.lableForTitle.text = model1.title;
    
    //像是这组图片的总个数和当前显示是第几张图片
    self.lableForPage.text = [NSString stringWithFormat:@"%ld/%ld", self.number + 1, self.arrForModels.count];
}




#pragma mark 创建UITextView
- (void)createTextView
{
    
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 560, self.view.frame.size.width, self.view.frame.size.height - 560)];
    
    self.textView.backgroundColor = [UIColor clearColor];
    
    self.textView.textColor = [UIColor whiteColor];
    
    [self.view addSubview:self.textView];
    
    self.textView.font = [UIFont systemFontOfSize:22];
    
    //禁止textView的编辑功能;
    self.textView.editable = NO;
    
    [_textView release];
}




#pragma mark 创建标题
- (void)createLableForTitleAndPage
{
    self.lableForTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 530, 300, 30)];
    
    self.lableForPage = [[UILabel alloc] initWithFrame:CGRectMake(300, 530, 114, 30)];
    
    self.lableForTitle.textColor = [UIColor whiteColor];
    
    self.lableForPage.textColor = [UIColor whiteColor];
    
    self.lableForPage.textAlignment = NSTextAlignmentRight;
    
    [self.lableForTitle setFont:[UIFont fontWithName:@"Helvetica-Bold" size:22]];
    
    self.lableForPage.font = [UIFont boldSystemFontOfSize:20];
    
    [self.view addSubview:self.lableForTitle];
    
    [self.view addSubview:self.lableForPage];
    
    [_lableForTitle release];
    
    [_lableForPage release];
}





#pragma mark UIScrollViewDelegate 的协议方法 >>>>>>
#pragma mark Scroll完成拖动的时候调用此方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //根据scrollView的内容偏移量计算出当前的页数
    self.path  = (NSInteger)self.scrollView.contentOffset.x / 414;
    
    ModelForForPhotoDetail *model = self.arrForModels[self.path];
    
    NSString *strForContent = model.content;
    
    NSString *strForAuthor = model.author;
    
    self.textView.text = model.content;
    
    self.textView.text = [NSString stringWithFormat:@"%@\n(摄影:%@)", strForContent,strForAuthor];
    
    self.lableForTitle.text = model.title;
    
    self.lableForPage.text = [NSString stringWithFormat:@"%ld/%ld", self.path + 1, self.arrForModels.count];
}


@end
